package com.dg11185.nearshop.activity.home.actys;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.imageloader.core.DisplayImageOptions;
import com.dg11185.libs.imageloader.core.ImageLoader;
import com.dg11185.libs.imageloader.core.display.FadeInBitmapDisplayer;
import com.dg11185.libs.utils.date.DateUtils;
import com.dg11185.nearshop.activity.home.WebviewActivity;
import com.dg11185.nearshop.app.DefaultFactory;
import com.dg11185.nearshop.net.weixin.Tabloid;

/**
 * 具有两个不同item的adapter，比原来的多了getItemViewType和getViewTypeCount这两个方法;
 * 
 * 参考链接： http://www.cnblogs.com/devinzhang/archive/2012/07/02/2573554.html
 * http:/
 * /stackoverflow.com/questions/4777272/android-listview-with-different-layout
 * -for-each-row
 * 
 * @author：Daibo
 * @time：2014-8-27 下午3:28:21
 */
public class TabloidAdapter extends BaseAdapter {

    public static final int TYPE_1 = 0;

    public static final int TYPE_2 = 1;

    private LayoutInflater mInflater;

    private List<Tabloid> tabloids;

    private Context context;

    private DisplayImageOptions options;

    public TabloidAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        options = new DisplayImageOptions.Builder().cacheOnDisc().showImageOnFail(R.drawable.web_image_background)
                .showImageForEmptyUri(R.drawable.web_image_background).displayer(new FadeInBitmapDisplayer(300))
                .resetViewBeforeLoading().build();
    }

    public void setTabloids(List<Tabloid> tabloids) {
        this.tabloids = tabloids;
    }

    public void addTabloids(List<Tabloid> tabloids) {

        if (tabloids.addAll(this.tabloids)) {
            this.tabloids = tabloids;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (tabloids == null) {
            return 0;
        }
        return tabloids.size();
    }

    @Override
    public Object getItem(int arg0) {
        return tabloids.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    /**
     * 根据数据源的position返回需要显示的的layout的type
     * */
    @Override
    public int getItemViewType(int position) {

        if (position == 0 || (!tabloids.get(position).remark.equals(tabloids.get(position - 1).remark))) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    /**
     * 返回所有的layout的数量
     * */
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        Tabloid tabloid = tabloids.get(position);
        int type = getItemViewType(position);
        ViewHolderHead headHolder = null;
        ViewHolderBottom bottomHolder = null;

        if (convertView == null) {
            switch (type) {
            case TYPE_1:
                headHolder = new ViewHolderHead();
                convertView = mInflater.inflate(R.layout.tabloid_item1, null);
                headHolder.txtTime = (TextView) convertView.findViewById(R.id.item_time);
                headHolder.txtView = (TextView) convertView.findViewById(R.id.item_head_txt);
                headHolder.imgView = (ImageView) convertView.findViewById(R.id.item_head_img);
                convertView.setTag(headHolder);
                break;
            case TYPE_2:
                bottomHolder = new ViewHolderBottom();
                convertView = mInflater.inflate(R.layout.tabloid_item2, null);
                bottomHolder.clickLyt = (LinearLayout) convertView.findViewById(R.id.click_llyt);
                bottomHolder.imgView = (ImageView) convertView.findViewById(R.id.item_img);
                bottomHolder.txtView = (TextView) convertView.findViewById(R.id.item_txt);
                bottomHolder.marginView = (TextView) convertView.findViewById(R.id.item_margin_view);
                convertView.setTag(bottomHolder);
                break;
            default:
                break;
            }

        } else {
            switch (type) {
            case TYPE_1:
                headHolder = (ViewHolderHead) convertView.getTag();
                break;
            case TYPE_2:
                bottomHolder = (ViewHolderBottom) convertView.getTag();
                break;
            default:
                break;
            }

        }
        if (type == TYPE_1) {
            if (position != 0) {
                String curTime = tabloid.createTime.substring(0, 10);
                String preTime = tabloids.get(position - 1).createTime.substring(0, 10);
                if (curTime.equals(preTime)) {
                    headHolder.txtTime.setVisibility(View.GONE);
                } else {
                    headHolder.txtTime.setVisibility(View.VISIBLE);
                    headHolder.txtTime.setText(getTimeStr(tabloid.createTime));
                }
            } else {
                headHolder.txtTime.setVisibility(View.VISIBLE);
                headHolder.txtTime.setText(getTimeStr(tabloid.createTime));
            }
            ImageLoader.getInstance().displayImage(tabloid.picUrl, headHolder.imgView, options);
            headHolder.txtView.setText(tabloid.description);
            headHolder.imgView.setOnClickListener(new ClickListener(position));
        } else {
            ImageLoader.getInstance().displayImage(tabloid.picUrl, bottomHolder.imgView,
                    DefaultFactory.getImageoptions());
            bottomHolder.txtView.setText(tabloid.description);
            bottomHolder.clickLyt.setOnClickListener(new ClickListener(position));
            if (position == getCount() - 1) {
                bottomHolder.marginView.setVisibility(View.VISIBLE);
            }else{
                bottomHolder.marginView.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    class ViewHolderHead {
        private TextView txtTime;

        private TextView txtView;

        private ImageView imgView;
    }

    class ViewHolderBottom {
        private LinearLayout clickLyt;

        private TextView txtView;

        private TextView marginView;

        private ImageView imgView;
    }

    private class ClickListener implements OnClickListener {

        private int position;

        public ClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, WebviewActivity.class);
            intent.putExtra("webviewUrl", tabloids.get(position).webUrl);
            context.startActivity(intent);
        }
    }

    private String getTimeStr(String time) {
        if (time.length() < 18) {
            return "";
        }
        StringBuilder builder = new StringBuilder("");
        long disDay = DateUtils.getDisDays(time.substring(0, 10), DateUtils.getCurrentDate());
        if (disDay > 3) {
            builder.append(time.substring(5, 7)).append("月").append(time.substring(8, 10)).append("日");
        } else if (disDay == 3) {
            builder.append("前天");
        } else if (disDay == 2) {
            builder.append("昨天");
        }

        int hour = Integer.parseInt(time.substring(11, 13));
        if (hour < 12) {
            builder.append(" 早上");
        } else if (hour < 20) {
            builder.append(" 下午");
        } else {
            builder.append(" 晚上");
        }
        builder.append(time.substring(11, 16));
        return builder.toString();
    }
}
